package com.htfyun.ft.config;

import android.text.TextUtils;

import com.htfyun.ft.utils.DebugLog;
import com.htfyun.ft.utils.SystemPropertyUtil;


/**
 * Created by sst on 2018/7/25.
 */

public final class FTProperty {

    private static final String TRUE = "true";
    private static final String FALSE = "false";

    /**
     * 该 property 将屏蔽掉按键功能.
     * 在 PhoneWindowManager.java 的 interceptKeyBeforeDispatching 函数里
     * let my app handle every key while in my factory test
     */
    private static final String HTFYUN_FT_ENABLED = "sys.htfyun.ft_enabled";

    public static void setHtfyunFTEnabled(boolean enabled) {
//        SystemPropertyUtil.setSystemProperty(HTFYUN_FT_ENABLED, enabled ? "1" : "0");
    }

    /**
     * pc 需要这个 persist 来判断 FT 是否测试ok
     */
    private static final String EINK_FT_TEST_ALL_RESULT = "persist.sys.ft.result";



    public static void setEinkFTTestAllResultPassed(boolean passed) {

        SystemPropertyUtil.setSystemProperty(EINK_FT_TEST_ALL_RESULT, passed ? TRUE : FALSE);

    }

    /**
     * 当这个为true时, 不再启动 此 apk.
     */
    private static final String EINK_PV_RO_EINKFT_DONE = "pv.ro.einkft.done";

    public static boolean isPvRoEinkFTDone() {
        final String done = SystemPropertyUtil.getSystemProperty(EINK_PV_RO_EINKFT_DONE);
        return TRUE.equalsIgnoreCase(done);
    }


//    //android bug collection
//    private static final String ABC_SWITCH = "persist.sys.abc_switch";
//    public static void setAbcSwitchEnabled() {
//
//        SystemPropertyUtil.setSystemProperty(ABC_SWITCH, "1");
//
//    }

    /**
     * persist.sys.boot.count > 1 是异常重启的错误。
     */
    private static final String ERROR_BOOT_COUNT = "persist.sys.boot.count";
    private static final String ERROR_BOOT_NEED_TEST = "persist.sys.boot.count_need_test";
    public static int getErrorBootCount() {

        String errorBootCntString = SystemPropertyUtil.getSystemProperty(ERROR_BOOT_COUNT);
        if (TextUtils.isEmpty(errorBootCntString)) {
            return 0;
        }
        if (!TextUtils.isDigitsOnly(errorBootCntString)) {
            DebugLog.e("errorBootCntString is not digit, which is " + errorBootCntString);
            return 0;
        }

        return Integer.parseInt(errorBootCntString) - 1;
    }

    public static void clearErrorBootCount() {
        SystemPropertyUtil.setSystemProperty(ERROR_BOOT_COUNT, "1");
    }

    public static boolean isErrorBootCount() {
        String countNeedTestString = SystemPropertyUtil.getSystemProperty(ERROR_BOOT_NEED_TEST);
        if (TextUtils.isEmpty(countNeedTestString)) {
            return false;
        }

        if (!TextUtils.isDigitsOnly(countNeedTestString)) {
            return false;
        }

        int need = Integer.parseInt(countNeedTestString);
        if (need > 0) {
            return getErrorBootCount() > 0;
        }

        return false;
    }

    /**
     *启动 cpuTest service 且判断 "persist.sys.cputest.result" 属性：starting, ok, fail.
     * 启动服务可以通过设置属性：setprop  ctl.start cpuTest
     * 停止服务可以通过设置属性: setprop  ctl.stop cpuTest
     */
    public enum CpuTestResultAttr {
        running0("running0"),
        running1("running1"),
        ok("ok"),
        fail("fail")
        ;
        private final String result;

        CpuTestResultAttr(String result) {
            this.result = result;
        }

        public String getResult() {
            return result;
        }
    }

    private static final String RESULT_CPU_TEST = "persist.sys.cputest.result";

    public static String getResultCpuTest() {
        return SystemPropertyUtil.getSystemProperty(RESULT_CPU_TEST);
    }

    public static CpuTestResultAttr getCpuTestResultAttr() {
        CpuTestResultAttr attr = CpuTestResultAttr.running0;

        String result = getResultCpuTest();

//        DebugLog.e("cpuTest: getCpuTestResult = " + result);

        for (CpuTestResultAttr resultAttr : CpuTestResultAttr.values()) {
            if (result.equalsIgnoreCase(resultAttr.getResult())) {
                attr = resultAttr;
            }
        }

        return attr;
    }

    public static void clearCpuTestResult() {
        SystemPropertyUtil.setSystemProperty(RESULT_CPU_TEST, CpuTestResultAttr.ok.getResult());
    }

    public static void startOrStopCpuTest(boolean start) {
        final String value = "cpuTest";
        final String propNameStart = "ctl.start";
        final String propNameStop = "ctl.stop";

        if (start) {
            SystemPropertyUtil.setSystemProperty(propNameStart, value);
            DebugLog.e("songshitian cpuTest: start Cpu Test");
        } else {
            SystemPropertyUtil.setSystemProperty(propNameStop, value);
            DebugLog.e("songshitian cpuTest: stop Cpu Test");
        }
    }

    //通过 "sys.cputest.memsize" 256 来设置memtest的大小，单位是 M。
    private static final String SET_CPUTEST_MEMSIZE = "sys.cputest.memsize";

    public static void setCpuTestMemsize(int size_m) {
        SystemPropertyUtil.setSystemProperty(SET_CPUTEST_MEMSIZE, String.valueOf(size_m));
    }

    public static void enterBootloader() {
        String cmd = "sys.powerctl";
        String value = "reboot,loader";
        SystemPropertyUtil.setSystemProperty(cmd, value);
    }

    /**
     * ro.sys.mem.size=1.8 \
     * ro.sys.emmc.size=28 \
     */
    private static final String MEM_SIZE = "ro.sys.mem.size";
    private static final String EMMC_SIZE = "ro.sys.emmc.size";

    public static float getRamSize() {
        String size = SystemPropertyUtil.getSystemProperty(MEM_SIZE);
        if (TextUtils.isEmpty(size)) {
            return -1.0f;
        }

        try {
            return Float.parseFloat(size);
        } catch (Exception e) {
            return -1.0f;
        }
    }

    public static float getRomSize() {
        String size = SystemPropertyUtil.getSystemProperty(EMMC_SIZE);
        if (TextUtils.isEmpty(size)) {
            return -1.0f;
        }

        try {
            return Float.parseFloat(size);
        } catch (Exception e) {
            return -1.0f;
        }
    }

}
